package com.hontech.bv.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bv.R

class PipeLineSensorView(ctx: Context, attrs: AttributeSet) : FrameLayout(ctx, attrs) {

    private val swMaxDoor: SwitchSensorView
    private val swMinDoor: SwitchSensorView
    private val swLock: SwitchSensorView
    private val swDropCup1: SwitchSensorView
    private val swDropCup2: SwitchSensorView
    private val swStepOrigin: SwitchSensorView
    private val swStepFault: SwitchSensorView
    private val swIce: SwitchSensorView
    private val swPearl: SwitchSensorView
    private val swPowder: SwitchSensorView
    private val swCup1: SwitchSensorView
    private val swCup2: SwitchSensorView
    private val numAdc: NumberSensorView
    private val btnHide: Button

    init {
        val view = LayoutInflater.from(ctx).inflate(R.layout.view_pipe_line_sensor, this)

        btnHide = view.findViewById(R.id.id_pipe_line_sensor_btn_hide)
        swMaxDoor = view.findViewById(R.id.id_pipe_line_sensor_max_door_close)
        swMinDoor = view.findViewById(R.id.id_pipe_line_sensor_min_door_close)
        swLock = view.findViewById(R.id.id_pipe_line_sensor_lock)
        swDropCup1 = view.findViewById(R.id.id_pipe_line_sensor_drop_cup1)
        swDropCup2 = view.findViewById(R.id.id_pipe_line_sensor_drop_cup2)
        swStepOrigin = view.findViewById(R.id.id_pipe_line_sensor_step_origin)
        swStepFault = view.findViewById(R.id.id_pipe_line_sensor_step_fail)
        swIce = view.findViewById(R.id.id_pipe_line_sensor_station_ice)
        swPearl = view.findViewById(R.id.id_pipe_line_sensor_station_drop_pearl)
        swPowder = view.findViewById(R.id.id_pipe_line_sensor_station_drop_powder)
        swCup1 = view.findViewById(R.id.id_pipe_line_sensor_station_drop_cup1)
        swCup2 = view.findViewById(R.id.id_pipe_line_sensor_station_drop_cup2)
        numAdc = view.findViewById(R.id.id_pipe_line_sensor_adc)

        hide()

        btnHide.setOnClickListener {
            if (swMaxDoor.visibility == VISIBLE) {
                hide()
            } else {
                show()
            }
        }
    }

    private fun show() {
        swMaxDoor.visibility = VISIBLE
        swMinDoor.visibility = VISIBLE
        swLock.visibility = VISIBLE
        swDropCup1.visibility = VISIBLE
        swDropCup2.visibility = VISIBLE
        swStepOrigin.visibility = VISIBLE
        swStepFault.visibility = VISIBLE
        swIce.visibility = VISIBLE
        swPearl.visibility = VISIBLE
        swPowder.visibility = VISIBLE
        swCup1.visibility = VISIBLE
        swCup2.visibility = VISIBLE
        numAdc.visibility = VISIBLE

        btnHide.text = "隐藏"
    }

    private fun hide() {
        swMaxDoor.visibility = GONE
        swMinDoor.visibility = GONE
        swLock.visibility = GONE
        swDropCup1.visibility = GONE
        swDropCup2.visibility = GONE
        swStepOrigin.visibility = GONE
        swStepFault.visibility = GONE
        swIce.visibility = GONE
        swPearl.visibility = GONE
        swPowder.visibility = GONE
        swCup1.visibility = GONE
        swCup2.visibility = GONE
        numAdc.visibility = GONE

        btnHide.text = "显示"
    }
}